import {Page} from '@/base/Page';
import {MainTain} from '@/base/model/MainTain';
import {isEmpty} from '@/base/util/Strings';
import {getMaintainList} from "@/base/api/ServiceApi";
import {formatDate} from "@/base/util/Dates";

class Form {
  createBegin = '';
  createEnd = '';
  deviceSn = '';
  name = '';
  phone = '';
  date = [];
}

export class ApplyBloc {
  form = new Form();
  paged: Page<MainTain> = new Page<MainTain>([], 0, 1, 15, 0, []);
  loading = false;

  load(pageIndex: number, pageSize: number) {
    if (!isEmpty(this.form.date) && !isEmpty(String(this.form.date[0]))) {
      // @ts-ignore
      this.form.createBegin = formatDate(new Date(this.form.date[0].getTime()),"yyyy-MM-dd") || '';
      // @ts-ignore
      this.form.createEnd = formatDate(new Date(this.form.date[1].getTime() + 24 * 60 * 60 * 1000),"yyyy-MM-dd")|| '';
    } else {
      this.form.createBegin = '';
      this.form.createEnd = '';
    }
    this.loading = true;
    return getMaintainList({
      pageIndex,
      pageSize,
      type: 1,
      deviceSn: this.form.deviceSn,
      name: this.form.name,
      tel: this.form.phone,
      sortDirection: 'ASC',
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }

  reset() {
    this.form = new Form();
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);

  }
  // removeItem(dateItem: Colour) {
  //   this.loading = true;
  //   return deleteColour(dateItem.colourid)
  //     .finally(() => {
  //       this.loading = false;
  //     });
  // }

}
